package com.jeeplus.mob.service;

import com.alibaba.fastjson.JSON;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.mob.common.StringHelper;
import com.jeeplus.mob.common.WxUtil;
import com.jeeplus.mob.entity.mobpojo.UserInfo;
import com.jeeplus.mob.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 登录
     * @param userName
     * @param password
     * @return
     */
    public AjaxJson login(String userName,String password) {
        UserInfo userInfo = userInfoMapper.checkUserNameandPassword(userName, password);
        if (StringHelper.IsEmptyOrNull(userInfo)) {
            return AjaxJson.success("当前账户不存在，请联系管理员！");
        }
        return AjaxJson.success().put("data",userInfo);
    }

    /**
     * 修改密码
     * @param id
     * @param oldPassword
     * @param newPassword
     * @return
     */
    public AjaxJson updateLoginAccount(String id,String oldPassword,String newPassword) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(Integer.parseInt(id));
        if (!userInfo.getPassword().equals(oldPassword)) {
            return AjaxJson.success("旧密码错误，请重新输入！");
        }
        int rows = userInfoMapper.updateLoginAccountById(Integer.parseInt(id), newPassword, id);
        if (rows > 0) {
            return AjaxJson.success("修改成功！");
        } else {
            return AjaxJson.success("请重试，修改失败！");
        }
    }

    /**
     * 获取用户openId
     * @param dataMap
     * @return
     */
    public Map<String, Object> getOpenId(Map<String, Object> dataMap) {
        String appId = dataMap.get("appId").toString();
        String secret = dataMap.get("secret").toString();
        Map<String, Object> open = new HashMap<>();
        String WX_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        String requestUrl = WX_URL.replace("APPID", appId).// 填写自己的appid
                replace("SECRET", secret).replace("JSCODE", dataMap.get("code").toString()).// 填写自己的appsecret，
                replace("authorization_code", "authorization_code");
        // 调用get方法发起get请求，并把返回值赋值给returnvalue
        String returnvalue = WxUtil.GET(requestUrl);
        // 定义一个json对象。
        com.alibaba.fastjson.JSONObject convertvalue = new com.alibaba.fastjson.JSONObject();
        // 将得到的字符串转换为json
        convertvalue = (com.alibaba.fastjson.JSONObject) JSON.parse(returnvalue);
        // 把openid和sessionkey分别赋值给openid和sessionkey
        String openid = "";
        if (convertvalue.containsKey("openid")) {
            openid = (String) convertvalue.get("openid");
        } else {
            return AjaxJson.error(convertvalue.get("errmsg").toString());
        }
        String sessionkey = (String) convertvalue.get("session_key");// 定义两个变量存储得到的openid和session_key.

        open.put("openId", openid);
        open.put("sessionkey", sessionkey);
        open.put("unionid", (String) convertvalue.get("unionid"));
        return open;
    }
}
